<?php

/**
 * Companies main page
 * @package ModuleGestionContacts
 * @version 1.0b
 */

session_start();

// Configuration file
include 'configuration.php';
// Functions
include 'inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	header('Location: index.php');
	exit();
endif;

// Include all objects
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// ---------- START: HEAD
include $configuration['webroot'].'inc/head.php';
// ---------- END: HEAD

// ---------- START: MENU
include $configuration['webroot'].'inc/menu.php';
// ---------- END: MENU

// ---------- START: BODY

if (isset($_GET['sep'])) {
	$activitysector = new ActivitySector();
	$activitysector->Get($_GET['sep']);
}
$add = isset($_GET['sep']) ? ' - '.$activitysector->activitysectorId : '';
?>
<div id="title" style="background-image:url('css/img/companies.png')">Entreprises<?php echo $add; ?></div>

<?php 
if (!isset($_GET['operation'])):

	if (empty($_SESSION['search']['companies']))
		$params = Array(Array('companyId', '>', '0'));
	else 
		$params = Array(Array('name', 'LIKE', '%'.$_SESSION['search']['companies'].'%'));
	if (isset($_GET['sep']))
		array_push($params, Array('activitysectorId', '=', $_GET['sep']));
	
	$company = new Company();
	$companyList = $company->GetList($params, 'name'); ?>
	
	<div id="nav">
		<a href="#" id="details" class="nav">Détails</a>
		<span class="separator"></span>
		<a href="companies.php?operation=add" id="add" class="nav">Nouveau</a>
		<a href="#" id="delete" class="nav">Supprimer</a>
		<span class="separator"></span>
		<a href="#" id="sendmail" class="email">E-mail</a>
		<a href="#" id="merge" class="merge">Fusionner</a>
		<span class="separator"></span>
		<input type="text" id="search-field" class="search-field" value="<?php echo (!empty($_SESSION['search']['companies'])) ? $_SESSION['search']['companies'] : ''; ?>" />
		<a href="companies" id="search" class="search">Rechercher</a>
	</div>
	
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects">
		<tr>
			<th class="check"></th>
			<th>#</th>
			<th>Nom</th>
			<th>Contact principal</th>
			<th>Siège social</th>
			<th>Secteur d'activité</th>
		</tr>
		<?php
		if (count($companyList) > 0):
			foreach ($companyList as $company):
				$activitysector = $company->GetActivitysector();
				$contact = $company->GetContact();
				$address = $company->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
				$address = $address[0];
				$res  = '<tr>';
				$res .= '<td class="check"><input type="checkbox" value="'.$company->companyId.'" /></td>';
				$res .= '<td>'.$company->companyId.'</td>';
				$res .= '<td><strong>'.$company->name.'</strong></td>';
				$res .= '<td><a href="contacts.php?id='.$contact->contactId.'&operation=edit">'.$contact->lastname.' '.$contact->firstname.'</a></td>';
				$res .= '<td>'.$address->number.' '.$address->street.', '.$address->postalcode.' '.$address->city.'</td>';
				$res .= '<td>'.$activitysector->name.'</td>';
				$res .= '</tr>';
				echo $res;
			endforeach;
		else:
			echo '<tr><td colspan="6" class="ui-state-error">Aucune donnée</td></tr>';
		endif;
		?>
	</table>
	
	<div id="sub-nav">
		<a href="#" class="selectall">Tout cocher</a>
		<a href="#" class="selectnone">Tout décocher</a>
	</div>

	<div id="select_model" class="hidden"></div>
	
<?php
else:
	$company = new Company();
	$address = new Address();
	$activitysector = new ActivitySector();
	$contact = new Contact();
	
	// In case we are in an update state
	if (($_GET['operation'] == 'edit') && isset($_GET['id'])):
		$company->Get($_GET['id']);
		$address = $company->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
		if (count($address) > 0) $address = $address[0]; else $address = new Address();
		$activitysector = $company->GetActivitysector();
		$contact = $company->GetContact();
	endif;

	include 'forms/company.php';

endif;
// ---------- END: BODY

// ---------- START: FOOT
include $configuration['webroot'].'inc/foot.php';
// ---------- END: FOOT

// ---------- JAVASCRIPTS AND END
include $configuration['webroot'].'inc/js.php'; ?>
<script type="text/javascript" src="js/companies.js"></script>

</body>
</html>